using System;
using System.Collections;
using System.Collections.Generic;
using PoolSystem;
using UnityEngine;

public class PoolManager : MonoBehaviour
{
    [SerializeField] private Pool[] playerBulletPool;//玩家子弹对象池

    [SerializeField] private Pool[] enemyBulletPool;//敌人子弹对象池

    [SerializeField] private Pool[] playerEffectPool;//玩家特效对象池

    [SerializeField] private Pool[] enemyEffectPool;//敌人特效对象池

    [SerializeField] private Pool[] experienceItemPool;//经验物品特效对象池

    static Dictionary<GameObject, Pool> _poolDict;// 对象池字典,根据对象找到对应的对象池
    private void Start()
    {
        _poolDict = new Dictionary<GameObject, Pool>();
        //初始化对象池
        Initialize(playerBulletPool);
        Initialize(enemyBulletPool);
        Initialize(playerEffectPool);
        Initialize(enemyEffectPool);
        Initialize(experienceItemPool);
    }

#if UNITY_EDITOR
    //游戏停止运行时调用的函数
    private void OnDestroy()
    {
        CheckPoolSize(playerBulletPool);
        CheckPoolSize(enemyBulletPool);
        CheckPoolSize(playerEffectPool);
        CheckPoolSize(enemyEffectPool);
        CheckPoolSize(experienceItemPool);
    } 
#endif

    /// <summary>
    /// 检查对象池的容量
    /// </summary>
    /// <param name="pools"></param>
    void CheckPoolSize(Pool[] pools)
    {
        foreach (var pool in pools)
        {
            if (pool.RuntimeSize > pool.Size)
            {
                Debug.LogWarning(
                    string.Format
                        ("对象池 : {0} 的容量:{1} 小于运行时容量: {2}", pool.Prefab.name, pool.Size, pool.RuntimeSize));
            }
        }
    }

    /// <summary>
    /// 初始化对应的对象池，有很多对象池，比如玩家子弹对象池，特效对象池，敌人子弹对象池（敌人子弹也有不同类型的子弹的对象池）等
    /// </summary>
    /// <param name="pools"></param>
    void Initialize(Pool[] pools)
    {
        foreach (var pool in pools)
        {
            //在编译阶段才会运行的代码
#if UNITY_EDITOR
                if (_poolDict.ContainsKey(pool.Prefab))
                {
                    Debug.LogError("对象池字典中已经存在了该对象池 : " + pool.Prefab.name);
                    continue;
                }
#endif
            _poolDict.Add(pool.Prefab, pool);//预制体和对象池的键值对
            Transform poolParent = new GameObject("Pool: " + pool.Prefab.name).transform;

            poolParent.parent = transform;
            pool.Initialize(poolParent);
        }
    }
    
    /// <summary>
    /// 根据传入的预制体，释放出一个对象
    /// </summary>
    /// <param name="prefab"></param>
    /// <returns></returns>
    public static GameObject Release(GameObject prefab)
    {
#if UNITY_EDITOR
        if (!_poolDict.ContainsKey(prefab))
        {
            Debug.LogError("对象池字典中不存在该对象池 : " + prefab.name);
            return null;
        }
#endif

        return _poolDict[prefab].PreparedObject();
    }
    
    public static GameObject Release(GameObject prefab, Vector3 position)
    {
#if UNITY_EDITOR
        if (!_poolDict.ContainsKey(prefab))
        {
            Debug.LogError("对象池字典中不存在该对象池 : " + prefab.name);
            return null;
        }
#endif

        return _poolDict[prefab].PreparedObject(position);
    }
    
    public static GameObject Release(GameObject prefab, Vector3 position, Quaternion rotation)
    {
#if UNITY_EDITOR
        if (!_poolDict.ContainsKey(prefab))
        {
            Debug.LogError("对象池字典中不存在该对象池 : " + prefab.name);
            return null;
        }
#endif

        return _poolDict[prefab].PreparedObject(position, rotation);
    }
    
    public static GameObject Release(GameObject prefab, Vector3 position, Quaternion rotation, Vector3 localScale)
    {
#if UNITY_EDITOR
        if (!_poolDict.ContainsKey(prefab))
        {
            Debug.LogError("对象池字典中不存在该对象池 : " + prefab.name);
            return null;
        }
#endif

        return _poolDict[prefab].PreparedObject(position, rotation, localScale);
    }
    

}
